package com.ruoyi.common.utils.order;

import com.ruoyi.common.core.redis.RedisCache;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;
import org.apache.commons.lang3.StringUtils;
import com.ruoyi.common.utils.spring.SpringUtils;

/**
 * 获取序号类
 * 
 * @author ruoyi
 */
public class OrderUtils
{
    private static final Logger log = LoggerFactory.getLogger(OrderUtils.class);

    public static synchronized String getOrderNumber(String prefix, String separator)
    {
        String key = getDateKey();
        Integer value = (Integer) SpringUtils.getBean(RedisCache.class).getCacheObject(key);
        if(value == null){
            value = 1;
        }else{
            value++;
        }
        SpringUtils.getBean(RedisCache.class).setCacheObject(key, value, 1, TimeUnit.DAYS);
        String seq = StringUtils.leftPad(String.valueOf(value), 4, "0");
        StringBuffer sb = new StringBuffer();
        if(prefix != null){
            sb.append(prefix);
        }
        if(separator != null){
            sb.append(separator);
        }
        if(key != null){
            sb.append(key);
        }
        if(separator != null){
            sb.append(separator);
        }
        if(seq != null){
            sb.append(seq);
        }
        //log.debug("QQQ " + "getOrderNumber = " + sb.toString());
        return sb.toString();
    }

    private static String getDateKey() {
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        Date today = new Date();
        String date = format.format(today);
        return date;
    }

}
